import type { BaseResponse } from "@/api/response";
import { http } from "@/utils/http";
import type { EventList } from "./types";
import {
  k8sPathMergeReqs,
  type MakePathReq,
  type MergePathReq
} from "./apiHelper";

export class K8sEventRepo {
  eventUrl = cluster_name => {
    return `/api/v1/proxy/${cluster_name}/k8s/api/v1/events`;
  };
  eventUrlWithNs = (cluster_name, namespace) => {
    return `/api/v1/proxy/${cluster_name}/k8s/api/v1/namespaces/${namespace}/events`;
  };

  baseReq: MakePathReq = {
    src: "events",
    app: ""
  };

  listEvents(req: MergePathReq): BaseResponse<EventList> {
    const api = k8sPathMergeReqs(req, this.baseReq);

    const params = Object.assign(
      {
        sortBy: ".metadata.creationTimestamp"
      },
      req.query ?? {}
    );
    return http.get(api, { params });
  }

  listEventsWithNsSelector(
    cluster_name,
    namespace,
    selectors
  ): BaseResponse<EventList> {
    let url = this.eventUrlWithNs(cluster_name, namespace);
    const param: any = {};
    if (selectors && selectors !== "") {
      param.fieldSelector = selectors;
    }
    return http.get(url, param);
  }

  listEventsWithPodSelector(cluster_name, namespace, fieldSelector) {
    let url = this.eventUrlWithNs(cluster_name, namespace);
    const param: any = {};
    if (fieldSelector && fieldSelector !== "") {
      param.fieldSelector = fieldSelector;
    }
    return http.get(url, param);
  }
}
